//
//  MyTableViewCell.m
//  SmartRoadShow
//
//  Created by Tracy E on 10-9-19.
//  Copyright 2010 simonwang142232@gmail.com. All rights reserved.
//

#import "MyTableViewCell.h"
#import "CacheManager.h"
#import <QuartzCore/QuartzCore.h>

@implementation MyTableViewCell
@synthesize delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
		
		self.backgroundColor = [UIColor clearColor];
		self.layer.contents = (id)[UIImage imageNamed:@"tableViewCell.png"].CGImage;
		
	    imageView = [[UIImageView alloc] initWithFrame:CGRectMake(25, 20, 80, 80)];
		[self addSubview:imageView];
		imageView.layer.cornerRadius = 5;
		imageView.layer.masksToBounds = YES;
		[imageView setBackgroundColor:[UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.8]];
		imageView.contentMode = UIViewContentModeScaleAspectFit;

		
		titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 15, 180, 35)];
		[titleLabel setNumberOfLines:0];
		[titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
		[self addSubview:titleLabel];
		
		detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 50, 180, 45)];
		[detailLabel setNumberOfLines:0];
		[detailLabel setFont:[UIFont boldSystemFontOfSize:11]];
		[detailLabel setTextColor:[UIColor grayColor]];
		[self addSubview:detailLabel];
		
		
		UIButton *accessory = [UIButton buttonWithType:UIButtonTypeCustom];
		[accessory addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
		[accessory setFrame:CGRectMake(270, 90, 30, 30)];
		[accessory setImage:[UIImage imageNamed:@"accessory.png"] forState:UIControlStateNormal];
		[self addSubview:accessory];
    }
    return self;
}

- (void)buttonPressed{
	[delegate seeDetailInfo:tag];
}

- (void)setTag:(NSInteger) aTag{
	tag = aTag;
}

- (void)setImage:(UIImage *)image{
	[imageView setImage:image];
}

- (void)setImageWithURL:(NSString *)url{
	
	UIImage *image = [CacheManager getImageAccordingToURLString:url];
	if (image != nil) {
		
		[imageView setBackgroundColor:[UIColor clearColor]];
		[imageView  setImage:image];	
	}
	else{
		
		ImageDownloader* downLoader = [[[ImageDownloader alloc] init] autorelease];
		downLoader.tag = tag;
		downLoader.imageReceiver = self;
		downLoader.imageURLString = url;
		[downLoader startDownloadWithImageURLString:url];
		
//		UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
//		[imageView setImage:image];
//		[CacheManager setImage:image withImageURLString:url];
	}
	
}

- (void)setTitleLabelText:(NSString *)title{
	[titleLabel setText:title];
}
- (void)setDetailLabelText:(NSString *)detail{
	[detailLabel setText:detail];
}
- (void)setPlaceLabelText:(NSString *)place{
	[placeLabel setText:place];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}


-(void)imageDownloader:(ImageDownloader*)downloader didReceiveImage:(UIImage*)image{
	
	NSLog(@"recivie");
	[imageView setBackgroundColor:[UIColor clearColor]];
	[imageView setImage:image];
	[CacheManager setImage:image withImageURLString:downloader.imageURLString];
}

- (void)dealloc {
    [super dealloc];
}


@end
